﻿using System.Linq;
using Enyim.Caching;
using Ayatta.Contract;
using Web.Pay.Models;
using System.Web.Mvc;
using Web.Base.Security;
using Ayatta.Contract.Domain;
using System.Collections.Generic;

namespace Web.Pay.Controllers
{
    public class HomeController : PayBaseController
    {
        public HomeController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient) : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index(string param)
        {
            var model = new HomeControllerModel.Index();

            var userId = User.Identity.Identity().Id;

            var platforms = GetAllPaymentPlatform();
            var tmpEBanks =  PaymentService.GetAllPaymentEBank(null, true);

            var eBanks = new List<PaymentEBank>();
            foreach (var platform in platforms)
            {
                var p = platform;
                var x = eBanks.Select(o => o.BankId);
                var tmp = tmpEBanks.Where(o => o.PlatformId == p.Id && !x.Contains(o.BankId));
                eBanks.AddRange(tmp.SkipWhile(o => !o.Status));
            }
            model.Trade = PaymentService.GetUserTrade(param, userId);
            model.Order = OrderService.GetOrder(param, userId, false, true);
            model.Platforms = platforms;
            model.EBanks = eBanks;
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(string param, int onlinePayment,bool balance, int? bank, int? platform)
        {
            /*
             付款方式多种多样，货到付款，网上支付，邮局汇款，银行转帐，帐户余额支付，当当荣誉卡支付，支票支付。
            Payment methods varied, Delivery, online payment, post office remittances, bank transfer, account balance of payments, Dangdang honor card payment by cheque
             */

            var url = DefaultUrl;
            var identity = User.Identity.Identity();
            var order = OrderService.GetOrder(param, identity.Id);
           // if(order.PaymentStatus==)
            var account = PaymentService.GetUserAccount(identity.Id);
          

            var pay = GetOnlinePay(bank.Value);

            if (pay != null)
            {
                
                var trade = new PayTrade();
                url = pay.GetPayUrl(trade);
                return Redirect(url);
            }
            return Redirect(url);
        }
    }
}
